﻿function append_category() {
    show_edit_dlg();
}

function edit_category(e) {
    var node = $("#my_doc_category").tree("getSelected");
    if (node != null) { show_edit_dlg(node.Id); }
}

function show_edit_dlg(id) {
    var dlg = $dlg('editCategoryDlgName');
    dlg.dialog({
        title: '分类信息', width: 600, height: 300, closed: false, cache: false, modal: true, maximizable: false, resizable: false, maximized: false,
        href: '~/MyNews/Doc/_EditCategoryDlg?ID='+id,
        onResize: function () { $(this).dialog('center'); },
        buttons: [
            {
                text: "确 定",
                handler: save_my_category,
                iconCls: "icon-save"
            }, {
                text: "取 消",
                handler: function () { $("#editCategoryDlgName").dialog("close"); },
                iconCls: "icon-cancel"
            }
        ],
        onLoad: function () {
            LoadObject(id, "~/MyNews/Doc/GetUserDocCategoryByID/", function (item) {
                var form = $("#editCategoryDlgName").find("form");
                form.form("load", item);
            });
        }
    });
}

function save_my_category() {
    var form = $("#editCategoryDlgName").find("form");
    if (form.form('enableValidation').form('validate')) {
        var menu = form.form_to_json();

        $.ajax({
            url: "~/MyNews/Doc/SaveUserDocCategory",
            type: "POST",
            data: menu,
            success: function (e) {
                if (e.Result) {
                    $('#editCategoryDlgName').dialog('close');
                    $("#my_doc_category").tree("reload");
                } else {
                    $.messager.alert('保存失败', '保存失败: ' + e.Msg, 'error');
                }
            },
            error: function (e) {
                ShowAjaxError(e);
            }
        });
    }
}

function remove_category() {
    var node = $("#my_doc_category").tree("getSelected");
    if (node != null) {
        if (node.Id > 0 && confirm("确认删除分类信息?")) {
            $.ajax({
                async: false,
                method: "DELETE",
                url: $.Content("~/MyNews/Doc/DeleteUserDocCategory/" + id),
                context: $("#@tableName"),
                success: function (e) {
                    if (e.Result) {
                        $("#my_doc_category").tree("reload");
                    } else {
                        $.messager.alert('删除数据错误', '删除失败: ' + e.Msg, 'error');
                    }
                },
                error: function (e1, e2, e3) {
                    ShowAjaxError(e1, "删除数据错误");
                    $("#my_doc_category").tree("reload");
                }
            });
        }
    }
}
